package com.fly.feishu.web.controller;

import com.fly.feishu.domain.enums.DataSyncType;
import com.fly.feishu.domain.service.EmpSyncService;
import com.fly.feishu.domain.service.EmployeeService;
import com.fly.feishu.infrastructure.persistent.po.Employee;
import com.fly.feishu.web.model.response.FeishuResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

@Controller
@RequestMapping("/employee")
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;
    @Autowired
    private EmpSyncService empSyncService;

    @GetMapping("/list")
    public ModelAndView listEmployeePage(ModelAndView modelAndView) {
        List<Employee> employees = employeeService.queryAll();
        modelAndView.addObject("employeeList", employees);
        modelAndView.setViewName("employee-list");
        return modelAndView;
    }

    @PostMapping("/syncEmp")
    @ResponseBody
    public FeishuResponse syncEmp(@RequestParam Long id, @RequestParam String type) {
        try {
            empSyncService.sync(id, DataSyncType.valueOf(type));
            return FeishuResponse.ok();
        } catch (Exception e) {
            return FeishuResponse.error(500, e.getMessage());
        }

    }

    @PutMapping("/edit/{id}")
    @ResponseBody
    public FeishuResponse updateEmp(@PathVariable("id") Long id, @RequestBody Employee employee) {
        employee.setId(id);
        try {
            employeeService.updateSelective(employee);
            empSyncService.sync(id, DataSyncType.PUT);
            return FeishuResponse.ok();
        } catch (Exception e) {
            return FeishuResponse.error(500, e.getMessage());
        }
    }

    @PostMapping("/create")
    @ResponseBody
    public FeishuResponse create(@RequestBody Employee employee) {
        try {
            employeeService.save(employee);
            empSyncService.sync(employee.getId(), DataSyncType.POST);
            return FeishuResponse.ok();
        } catch (Exception e) {
            return FeishuResponse.error(500, e.getMessage());
        }
    }

    /**
     * 离职
     * @param id
     * @return
     */
    @DeleteMapping("/leave/{id}")
    @ResponseBody
    public FeishuResponse leave(@PathVariable("id") Long id) {
        Employee employee = new Employee();
        employee.setId(id);
        employee.setStatus("I");
        try {
            employeeService.updateSelective(employee);
            empSyncService.sync(id, DataSyncType.DELETE);
            return FeishuResponse.ok();
        } catch (Exception e) {
            return FeishuResponse.error(500, e.getMessage());
        }
    }

    /**
     * 员工离职后恢复入职
     * @param id
     * @return
     */
    @PostMapping("/renew/{id}")
    @ResponseBody
    public FeishuResponse renew(@PathVariable("id") Long id) {
        Employee employee = new Employee();
        employee.setId(id);
        employee.setStatus("A");
        try {
            employeeService.updateSelective(employee);
            empSyncService.sync(id, DataSyncType.POST);
            return FeishuResponse.ok();
        } catch (Exception e) {
            return FeishuResponse.error(500, e.getMessage());
        }
    }

}
